package com.yx.cloud.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.iot.product.sweeper.bean.CameraSettings;
import com.iot.sweeper.AppContext;
import com.iot.sweeper.param.CleanSettingParam;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.RealtimevideoActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.iot.product.sweeper.bean.Robot;
import com.iot.common.utils.Event;
import com.iot.common.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 实时视频
 */
public class RealTimeVideoActivity extends BaseActivity implements View.OnClickListener {
    private RealtimevideoActivityBinding binding;
    private Robot robot;
    private CameraSettings cameraSettings;

    @Override
    protected void onCreate() {
        super.onCreate();
        binding = RealtimevideoActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        initData();
    }

    private void initData() {
        robot = AppContext.get().getCurrentDevice();
        cameraSettings = robot.getCameraSettings();
        if (cameraSettings.isLiveVideoSwitch()) {
            binding.realtimeVodeoState.setBackgroundResource(R.mipmap.switch_track_on);
        } else {
            binding.realtimeVodeoState.setBackgroundResource(R.mipmap.switch_track_off);
        }

        binding.VodeoRemind.setChecked(cameraSettings.isVideoReminderSwitch());
        binding.VodeoRemind.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setVideoReminderSwitch(isChecked).build());
            }
        });

        binding.ivCameraRealTimeReturn.setOnClickListener(this);
        binding.rlSecuritySettings.setOnClickListener(this);
        binding.rlVideoPolicy.setOnClickListener(this);
        binding.realtimeVodeoState.setOnClickListener(this);
        binding.rlSecuritySettings.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.iv_cameraRealTimeReturn) {
            Intent_utils.backIntent(this);
        } else if (id == R.id.rl_securitySettings) {
            //安全设置
            Intent_utils.enterIntent(this,SecuritySettingsActivity.class);
        } else if (id == R.id.rl_videoPolicy) {
            Intent_utils.enterIntentContext(this, VideoPolicyActivity.class, "Policy", "video");
        } else if (id == R.id.realtimeVodeoState) {
            if (cameraSettings.isLiveVideoSwitch()) {
                //仅可关闭
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setLiveVideoSwitch(false).build());
            } else {
                //
                realTimeVideoHintDialog(getString(R.string.MV_Video_LiveVideo_Title), getString(R.string.MV_Video_LiveVideo_DesInfo1));
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_CAMERASETTING:
                if (cameraSettings.isLiveVideoSwitch()) {
                    binding.realtimeVodeoState.setBackgroundResource(R.mipmap.switch_track_on);
                } else {
                    binding.realtimeVodeoState.setBackgroundResource(R.mipmap.switch_track_off);
                }
                binding.VodeoRemind.setChecked(cameraSettings.isVideoReminderSwitch());
                break;
        }
    }

    @SuppressLint("LongLogTag")
    private void realTimeVideoHintDialog(String dialogTitle, String content) {
        Dialog dialog = new Dialog(RealTimeVideoActivity.this);
        dialog.setContentView(R.layout.hint_dialog);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.setCanceledOnTouchOutside(false);
        TextView mBtnOutLoginConfirm = dialog.findViewById(R.id.btn_hintConfirm);
        TextView mTvDialogTitle = dialog.findViewById(R.id.tv_dialogHintTitle);
        TextView mTvContent = dialog.findViewById(R.id.tv_hint_content);
        mTvDialogTitle.setVisibility(View.VISIBLE);
        mTvDialogTitle.setText(dialogTitle);
        mTvContent.setText(content);
        mBtnOutLoginConfirm.setText(getString(R.string.U_Ok_Btn));

        mBtnOutLoginConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.i("11111111111111111111111111111: ");
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
